package com.jiangruan.javaproject.address;/**
 * @author:xulonghua
 * @email:3162193565@qq.com
 * desc:该功能用于比特币地址的校验
 * datatime 2021/12/21 14:55
 */

import com.jiangruan.javaproject.utils.Base58;
import com.jiangruan.javaproject.utils.Hash;

import java.util.Arrays;

/**
 *@author:xulonghua
 *@email:3162193565@qq.com
 *desc:
 *datetime 2021/12/21 14:55
 */
public class VerifyAddress {
    public  boolean CheckAddress(String address){
        if (address == null || "".equals ( address )){
            return false;
        }
        byte[] decode = Base58.decode ( address );
        if (decode.length<4){
            return  false;
        }
        byte [] check=new  byte[4];
        System.arraycopy ( decode,decode.length-4,check,0,check.length );
        byte[] ver_verify=new byte[decode.length-check.length];
        System.arraycopy ( decode,0,ver_verify,0,ver_verify.length );
        //双重计算
        byte[] hash1 = Hash.SHA256 ( ver_verify);
        byte[] hash2 = Hash.SHA256 ( hash1 );
        //取hash2前四位作为校验位于check作为校验
        //if check == check2 比特币合规的
        //if check ！= check2 无效的
        byte[] check2 =new byte[4];
        System.arraycopy ( hash2,0,check2,0,check2.length );
        boolean equals = Arrays.equals ( check, check2 );
//        if (check != check2){
//            return false;
//        }
//        return true;
        return equals;
    }
}
